package com.viish.android.apps.buildbot.job;

/* Android Buildbot Manager
Copyright (C) 2011  Sylvain "Viish" Berfini

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;

public class Builder 
{
	private String name;
	private String basedir;
	private String category;
	private List<Integer> cachedBuilds, currentBuilds, pendingBuilds;
	private List<String> slaves;
	private String state;
	
	public Builder(String name, String basedir, String category,
			List<Integer> cacheBuilds, List<Integer> currentBuilds,
			List<Integer> pendingBuilds, List<String> slaves, String state) {
		super();
		this.name = name;
		this.basedir = basedir;
		this.category = category;
		this.cachedBuilds = cacheBuilds;
		this.currentBuilds = currentBuilds;
		this.pendingBuilds = pendingBuilds;
		this.slaves = slaves;
		this.state = state;
	}

	public Builder()
	{
		super();
	}
	
	public Builder (Object key, JSONObject jsono)
	{
		super();
		this.name = key.toString();
		this.basedir = jsono.optString("basedir", null);
		JSONArray cachedBuilds = jsono.optJSONArray("cachedBuilds");
		this.cachedBuilds = BuildbotJSONParser.arrayToListInteger(cachedBuilds);
		this.category = jsono.optString("category", null);
		JSONArray currentBuilds = jsono.optJSONArray("currentBuilds");
		this.currentBuilds = BuildbotJSONParser.arrayToListInteger(currentBuilds);
		JSONArray pendingBuilds = jsono.optJSONArray("pendingBuilds");
		this.pendingBuilds = BuildbotJSONParser.arrayToListInteger(pendingBuilds);
		JSONArray slaves = jsono.optJSONArray("slaves");
		this.slaves = BuildbotJSONParser.arrayToListString(slaves);
		this.state = jsono.optString("state", null);
	}
	
	public String toString()
	{
		return "\"" + name + "\": { " 
		+ "\"basedir\": \"" + basedir + "\", " 
		+ "\"cachedBuilds\": " + cachedBuilds + ", "
		+ "\"category\": " + category + ", "
		+ "\"currentBuilds\": " + currentBuilds + ", " 
		+ "\"pendingBuilds\": " + pendingBuilds + ", "
		+ "\"slaves\": " + slaves + ", "
		+ "\"state\": " + state 
		+ " }";
	}
	
	public View getOverView(Context c)
	{
		TextView v = new TextView(c);
		v.setPadding(5, 0, 5, 0);
		v.setBackgroundColor(Color.parseColor("#88FF88"));
		v.setText(this.name);
		v.setTextColor(Color.BLACK);
		v.setGravity(Gravity.CENTER);
		return v;
	}

	public String getName() 
	{
		return name;
	}

	public void setName(String name) 
	{
		this.name = name;
	}

	public String getBasedir() 
	{
		return basedir;
	}

	public void setBasedir(String basedir) 
	{
		this.basedir = basedir;
	}

	public List<Integer> getCachedBuilds() 
	{
		return cachedBuilds;
	}

	public void setCachedBuilds(List<Integer> cachedBuilds) 
	{
		this.cachedBuilds = cachedBuilds;
	}

	public List<String> getSlaves() 
	{
		return slaves;
	}

	public void setSlaves(List<String> slaves) 
	{
		this.slaves = slaves;
	}

	public String getState() 
	{
		return state;
	}

	public void setState(String state) 
	{
		this.state = state;
	}

	public String getCategory() 
	{
		return category;
	}

	public void setCategory(String category)
	{
		this.category = category;
	}

	public List<Integer> getCurrentBuilds() 
	{
		return currentBuilds;
	}

	public void setCurrentBuilds(List<Integer> currentBuilds) 
	{
		this.currentBuilds = currentBuilds;
	}

	public List<Integer> getPendingBuilds()
	{
		return pendingBuilds;
	}

	public void setPendingBuilds(List<Integer> pendingBuilds) 
	{
		this.pendingBuilds = pendingBuilds;
	}
}
